package com.gitee.wsl.prism4j.languages

import com.gitee.wsl.prism4j.GrammarUtils
import com.gitee.wsl.prism4j.Prism4j
import com.gitee.wsl.prism4j.Prism4j.Companion.pattern
import com.gitee.wsl.prism4j.Prism4j.Companion.token
import com.gitee.wsl.prism4j.Prism4j.Pattern
import com.gitee.wsl.prism4j.annotations.Extend

@Suppress("unused")
@Extend("c")
object Prism_cpp {
    fun create(prism4j: Prism4j): Prism4j.Grammar {
        val cpp: Prism4j.Grammar = GrammarUtils.extend(
            GrammarUtils.require(prism4j, "c"),
            "cpp",
            token(
                "keyword",
                pattern(Pattern.compile("\\b(?:alignas|alignof|asm|auto|bool|break|case|catch|char|char16_t|char32_t|class|compl|const|constexpr|const_cast|continue|decltype|default|delete|do|double|dynamic_cast|else|enum|explicit|export|extern|float|for|friend|goto|if|inline|int|int8_t|int16_t|int32_t|int64_t|uint8_t|uint16_t|uint32_t|uint64_t|long|mutable|namespace|new|noexcept|nullptr|operator|private|protected|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_assert|static_cast|struct|switch|template|this|thread_local|throw|try|typedef|typeid|typename|union|unsigned|using|virtual|void|volatile|wchar_t|while)\\b"))
            ),
            token(
                "operator",
                pattern(Pattern.compile("--?|\\+\\+?|!=?|<{1,2}=?|>{1,2}=?|->|:{1,2}|={1,2}|\\^|~|%|&{1,2}|\\|\\|?|\\?|\\*|\\/|\\b(?:and|and_eq|bitand|bitor|not|not_eq|or|or_eq|xor|xor_eq)\\b"))
            )
        )

        // in prism-js cpp is extending c, but c has not booleans... (like classes)
        GrammarUtils.insertBeforeToken(
            cpp, "function",
            token("boolean", pattern(Pattern.compile("\\b(?:true|false)\\b")))
        )

        GrammarUtils.insertBeforeToken(
            cpp, "keyword",
            token(
                "class-name",
                pattern(
                    Pattern.compile("(class\\s+)\\w+", Pattern.CASE_INSENSITIVE),
                    true
                )
            )
        )

        GrammarUtils.insertBeforeToken(
            cpp, "string",
            token(
                "raw-string",
                pattern(
                    Pattern.compile("R\"([^()\\\\ ]{0,16})\\([\\s\\S]*?\\)\\1\""),
                    false,
                    true,
                    "string"
                )
            )
        )

        return cpp
    }
}
